import Tkinter as tk
import tkFileDialog as filedialog
import tkMessageBox as mb


class Menubar:
	
	def __init__(self,parent):
		
		self.test = tk.PhotoImage(file= 'test.png')
		self.openi = tk.PhotoImage(file='open.png')
		self.savei = tk.PhotoImage(file='save.png')
		self.saveas = tk.PhotoImage(file='saveas.png')
		self.exiti = tk.PhotoImage(file='exit.png')
		menubar = tk.Menu(parent.master)
		parent.master.config(menu=menubar)
		
		file_dropdown = tk.Menu(menubar)
		file_dropdown.add_command(label ="New File",command=parent.new_file, accelerator="Ctrl+N")
		file_dropdown.add_command(label ="Open File",command =parent.open_file,accelerator="Ctrl+O")
		file_dropdown.add_command(label ="Save",command =parent.save,accelerator="Ctrl+S")
		file_dropdown.add_command(label ="Save As",command=parent.save_as,accelerator="Ctrl+Shift+S")
		file_dropdown.add_separator()
		file_dropdown.add_command(label ="Exit",command=parent.master.destroy)
		
		##################in hindi################
		
		
		hindi_dropdown = tk.Menu(menubar)
		hindi_dropdown.add_command(label ="Naya File",command=parent.new_file, accelerator="Ctrl+N",image= self.test)
		hindi_dropdown.add_command(label ="File Kholna",command =parent.open_file,accelerator="Ctrl+O",image=self.openi)
		hindi_dropdown.add_command(label ="File bachana(save)",command =parent.save,accelerator="Ctrl+S",image=self.savei)
		hindi_dropdown.add_command(label ="File Rakhna(save as)",command=parent.save_as,accelerator="Ctrl+Shift+S",image=self.saveas)
		hindi_dropdown.add_separator()
		hindi_dropdown.add_command(label ="Baahir(Exit)",command=parent.master.destroy,image= self.exiti)
		
		
		about_dropdown = tk.Menu(menubar)
		about_dropdown.add_command(label="About",command= self.show_about_message)
		
		menubar.add_cascade(menu=file_dropdown,label="File")
		menubar.add_cascade(label="Fael(Hindi)",menu=hindi_dropdown )
		menubar.add_cascade(label="About/Jankari",menu=about_dropdown )
		
		
		
	def show_about_message(self):
		box_title = "About EJ"
		box_message = "It is a Text Editor called EJ or Editor JI. License: creative commons cc0 1.0(public domain). Version-0.5"
		mb.showinfo(box_title,box_message)
			

class Statusbar:
	def __init__(self,parent):
		self.status =tk.StringVar()	
		self.status.set("EditorJi - 0.5 ")
		
		label = tk.Label(parent.textarea, textvariable=self.status,fg="black",
		bg="white",anchor='sw')
		label.pack(side=tk.BOTTOM, fill =tk.BOTH)
		
	def update_status(self,*args):
		if isinstance(args[0],bool):
			self.status.set("Your File Is Saved ")
		else:
			self.status.set("EditorJi - 0.5 ")
	
class PyText:
	
	def __init__(self,master):
		master.title("Untitled -EJ")
		master.geometry("700x500")
		
		
		self.master = master
		self.filename = None
		
		self.textarea = tk.Text(master)
		self.scroll = tk.Scrollbar(master, command=self.textarea.yview)
		self.textarea.configure(yscrollcommand= self.scroll.set)
		self.textarea.pack(side=tk.LEFT,fill=tk.BOTH,expand=True)
		self.scroll.pack(side=tk.RIGHT , fill=tk.Y)
		
		self.menubar = Menubar(self)
		self.statusbar = Statusbar(self)
		
		self.bind_shirtcuts()
		
		
	def set_window_title(self,name=None):
		if name:
			self.master.title(name)
		else:
			self.master.title("EJ-Untitled")
		
	def new_file(self,*args):
		self.textarea.delete(1.0,tk.END)
		self.filename = None
		self.set_window_title()
		
		
	def open_file(self,*args):
		self.filename= filedialog.askopenfilename(
		defaultextension =".txt",
		filetypes=[("All Files","*.*"),
		           ("Text File","*.txt"),
		           ("Python file","*.py"),
		           ("FIG ","*.fig"),
		           ("FIG PLUS ","*.fgp"),
		           ("PAVUM","*.pav"),
		           ("JavaScript","*.js"),
		           ("HTML Documents","*.html"),
		           ("CSS  Document","*.css"),
		           ("Markdown Document","*.*")]
		)
		
		if self.filename:
			self.textarea.delete(1.0,tk.END)
			with open(self.filename, "r") as f:
				self.textarea.insert(1.0, f.read())
			self.set_window_title(self.filename + " -EJ")
		
	def save(self,*args):
		if self.filename:
			try:
				textarea_content = self.textarea.get(1.0,tk.END)
				with open(self.filename,"w") as f:
					f.write(textarea_content)
				self.statusbar.update_status(True)
			except Exception as e :
				print(e)
		else:
			self.save_as()
		
	def save_as(self,*args):
		try:
			new_file = filedialog.asksaveasfilename(
			initialfile="untitled.txt",
			defaultextension =".txt",
		    filetypes=[("All Files","*.*"),
		           ("Text File","*.txt"),
		           ("Python file","*.py"),
		           ("FIG ","*.fig"),
		           ("FIG PLUS ","*.fgp"),
		           ("PAVUM","*.pav"),
		           ("JavaScript","*.js"),
		           ("HTML Documents","*.html"),
		           ("CSS  Document","*.css"),
		           ("Markdown Document","*.*")]
			)
			
			textarea_content = self.textarea.get(1.0,tk.END)
			with open(new_file,"w") as f:
				f.write(textarea_content)
			self.filename= new_file
			self.set_window_title(self.filename)
			self.statusbar.update_status(True)
			
				
				
		except Exception as e:
			print(e)
			
	def bind_shirtcuts(self):
		self.textarea.bind('<Control-n>',self.new_file)
		self.textarea.bind('<Control-o>',self.open_file)
		self.textarea.bind('<Control-s>',self.save)
		self.textarea.bind('<Control-S>',self.save_as)
		self.textarea.bind('<Key>',self.statusbar.update_status)
		
if __name__ == "__main__":
	master =tk.Tk()
	pt = PyText(master)
	master.mainloop()

#############################################################################
##License: creatice commons cc0 1.0(public domain)	
##credit: fcr
#############################################################################
